﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using PhoneAppSTS.Utils;
using Microsoft.Phone.Scheduler;
using STS.Model;

namespace PhoneAppSTS
{
    public partial class Settings : PhoneApplicationPage
    {
        public Settings()
        {
            ApplicationBar = App.AppBar;
            InitializeComponent();
            this.DataContext = App.Settings;
        }

        private void beginTimePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            if (e.NewDateTime != null)
            {
                App.Settings.BeginTimeSetting = e.NewDateTime;
                App.Settings.MiddleTimeSetting = e.NewDateTime.Value.AddHours(6);
                App.Settings.LastTimeSetting = e.NewDateTime.Value.AddHours(12);
            }

            if (App.Settings.BeginIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.MORNING));
                CreateNotification(Constants.MORNING);
            }

            if (App.Settings.MiddleIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.MIDDAY));
                CreateNotification(Constants.MIDDAY);
            }

            if (App.Settings.LastIsCheckedSetting)
            {
                ScheduledActionService.Remove(DeleteNotification(Constants.EVENING));
                CreateNotification(Constants.EVENING);
            }
        }

        private void CreateNotification(int which)
        {
            String name = System.Guid.NewGuid().ToString();
            Reminder reminder = new Reminder(name);
            reminder.Title = "Przypomnienie o lekach!";
            reminder.Content = "Leki czas wziąć";
            DateTime dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);

            if (which == Constants.MORNING)
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.BeginTimeSetting.Value.Hour, App.Settings.BeginTimeSetting.Value.Minute, App.Settings.BeginTimeSetting.Value.Second);
                App.Settings.BeginNotificationNameSetting = name;
            }
            else if (which == Constants.MIDDAY)
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.MiddleTimeSetting.Value.Hour, App.Settings.MiddleTimeSetting.Value.Minute, App.Settings.MiddleTimeSetting.Value.Second);
                App.Settings.MiddleNotificationNameSetting = name;
            }
            else
            {
                reminder.BeginTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    App.Settings.LastTimeSetting.Value.Hour, App.Settings.LastTimeSetting.Value.Minute, App.Settings.LastTimeSetting.Value.Second);
                App.Settings.LastNotificationNameSetting = name;
            }
            if (reminder.BeginTime < DateTime.Now)
            {
                reminder.BeginTime = reminder.BeginTime.AddDays(1);
            }
            DateTime exp = reminder.BeginTime.AddMinutes(5);
            reminder.ExpirationTime = exp;
            reminder.RecurrenceType = RecurrenceInterval.Daily;

            reminder.NavigationUri = new Uri("/ShowMedicines.xaml?which=" + which, UriKind.Relative);

            // Register the reminder with the system.
            ScheduledActionService.Add(reminder);
        }

        private string DeleteNotification(int which)
        {
            if (which == Constants.MORNING)
            {
                return App.Settings.BeginNotificationNameSetting;
            }
            else if (which == Constants.MIDDAY)
            {
                return App.Settings.MiddleNotificationNameSetting;
            }
            else
            {
                return App.Settings.LastNotificationNameSetting;
            }
        }

        private void beginCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.BeginIsCheckedSetting = true;
            CreateNotification(Constants.MORNING);

        }

        private void beginCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.BeginIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.MORNING));
        }

        private void middleCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.MiddleIsCheckedSetting = true;
            CreateNotification(Constants.MIDDAY);
        }

        private void middleCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.MiddleIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.MIDDAY));
        }

        private void lastCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            App.Settings.LastIsCheckedSetting = true;
            CreateNotification(Constants.EVENING);
        }

        private void lastCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            App.Settings.LastIsCheckedSetting = false;
            ScheduledActionService.Remove(DeleteNotification(Constants.EVENING));
        }

        private void ButtonAktualizuj_Click(object sender, RoutedEventArgs e)
        {

            //DodajNoweLeki();
            //DodajNowePory();

        }

        private void DodajNowePory()
        {
            PoraIlosc poraIlosc;

            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 1;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(1);
            poraIlosc.Nazwa = Constants.MORNING;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);

            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 1;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(1);
            poraIlosc.Nazwa = Constants.EVENING;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);

            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 2;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(2);
            poraIlosc.Nazwa = Constants.MIDDAY;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);

            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 3;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(3);
            poraIlosc.Nazwa = Constants.MORNING;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);
            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 3;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(3);
            poraIlosc.Nazwa = Constants.MIDDAY;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);
            poraIlosc = new PoraIlosc();
            poraIlosc.Ilosc = 3;
            poraIlosc.Lek = ModelInstance.LekViewModel.FindLekById(3);
            poraIlosc.Nazwa = Constants.EVENING;
            ModelInstance.PoraIloscViewModel.AddPoraIlosc(poraIlosc);

        }

        private void DodajNoweLeki()
        {
            Lek lek;
            lek = new Lek();
            lek.Nazwa = "Lek 1";
            lek.Opis = "Opis leku 1. Opis leku 1.Opis leku 1.Opis leku 1.Opis leku 1.";
            lek.Url = "/Images/m2.jpg";
            ModelInstance.LekViewModel.AddLek(lek);

            lek = new Lek();
            lek.Nazwa = "Lek 2";
            lek.Opis = "Opis leku 2.";
            lek.Url = "/Images/m1.jpg";
            ModelInstance.LekViewModel.AddLek(lek);

            lek = new Lek();
            lek.Nazwa = "Lek 3";
            lek.Opis = "Opis leku 3.";
            lek.Url = "/Images/m3.jpg";
            ModelInstance.LekViewModel.AddLek(lek);


        }

        private void ButtonSprawdz_Click(object sender, RoutedEventArgs e)
        {
            ModelInstance.LekViewModel.FindLekItems();
            foreach (var item in ModelInstance.LekViewModel.LekItems)
            {
                Debug.WriteLine(item);
                if (item.PoraIloscArray.Count != 0)
                {
                    foreach (var item2 in item.PoraIloscArray)
                    {
                        Debug.WriteLine(item2);
                    }
                }
            }
        }
    }
}